<template>
	<view class="page">
		<view class="body">
			<view class="flex_column flex_center">
				<!-- 状态信息 -->
				<view class="topTab">
					<view class="dark flex_row">
						<text class="text1">桌号：{{deskId}}</text>
						<text class="text1">待支付：￥{{totalPrice}}</text>
					</view>
					<view class="tint flex_column">
						<view class="text2">共下单{{totalNum}}道菜品</view>
						<view class="flex_row button_group">
							<navigator url="../index/index">
								<button type="default" class="button1">加菜</button>
							</navigator>
							<button type="default" class="button2" @click="gotoPay">买单</button>
						</view>
					</view>
				</view>
			</view>
			<view class="gray"></view>
			<!-- 订单项列表 -->
			<view class="order_body">
				<!-- title -->
				<view class="title">
					<text class="title_f">点单详情</text>
					<text class="title_b">下单时间：{{orderTime}}</text>
				</view>
				<!-- 账单列表 -->
				<block v-for="(item,index) in orderList" :key="index">
					<view class="confirm_list">
						<text class="list_name">{{item.foodName}}</text>
						<text class="list_price">￥{{item.foodPrice.toFixed(1)}} ×{{item.sellCount}}</text>
					</view>
				</block>
			</view>
		</view>
		<!-- 底部 -->
		<view class="bottom_box flex_row">
			<navigator url="../index/index" class="flex_column half">
				<image class="icon1" src="../../static/food.png" background-size="cover"></image>
				<view class="text">去加菜</view>
			</navigator>
			<view class="flex_column half" @click="gotoPay">
				<image class="icon1" src="../../static/wallet.png" background-size="cover"></image>
				<view class="text">去买单</view>
			</view>
		</view>
	</view>
</template>

<script>
	import common from "../../common/common.js"
	export default {
		data() {
			return {
				deskId: 0,
				totalPrice: 0,
				totalNum: 0,
				orderList: [],
				orderTime: '',

			}
		},
		onLoad(option) {
			// console.log(option)
			this.deskId = option.deskId;
			// this.totalPrice = option.price;
			// this.totalNum = option.num;

			this.orderList = uni.getStorageSync('orderList')
			var orderId = uni.getStorageSync('orderId');
			// 重新计算总金额
			var total = common.getTotalPrice(this.orderList)
			this.totalNum = total[0];
			this.totalPrice = total[1];
			// console.log(this.orderList)
			var date = new Date()
			// console.log(date.getHours()+":"+date.getMinutes())
			var orderTime = date.getHours() + ":" + date.getMinutes()
			console.log(orderTime)
			this.orderTime = orderTime;

			// 更新后台数据
			uni.request({
				url: this.serverUrl + '/wUpdateOrder',
				method: 'GET',
				data: {
					orderId: orderId,
					orderCost: this.totalPrice
				},
				success: res => {},
				fail: () => {},
				complete: () => {}
			});
		},
		methods: {
			gotoPay(e) {
				var orderId = uni.getStorageSync('orderId')
				// 提示是否确认付款
				uni.showModal({
					title: '',
					content: '现在付款？',
					showCancel: true,
					cancelText: '再等等',
					confirmText: '确定',
					success: res => {
						if (res.confirm) {
							console.log('已付款');
							uni.showLoading({
								title: '付款中',
								mask: true
							});
							// 修改后台付款状态
							uni.request({
								url: this.serverUrl + '/wPayOrder',
								method: 'GET',
								data: {
									orderId
								},
								success: res => {
									if (res.data == 1) {
										console.log('完成支付')
										uni.hideLoading();
										uni.showToast({
											title: '付款成功！',
											icon: 'success',
										});
										// 清空数据
										common.orderComment = '';
										common.orderPeopleNum = 0;

										setTimeout(function() {
											// 跳转页面
											uni.redirectTo({
												url: '../review/review',
											});
										}, 2000);

									} else {
										console.log('支付失败')
										uni.hideLoading();
										uni.showToast({
											title: '付款失败',
											image: '../../static/fail.png'
										});
									}
								},
								fail: () => {},
								complete: () => {}
							});
						}
					},
					fail: () => {},
					complete: () => {}
				});
			}
		}
	}
</script>

<style>
	@import url("orderDetail.css");
</style>
